package com.blackcat.recode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blackcat.recode.common.result.AjaxResult;
import com.blackcat.recode.dao.UpdateRecodeMapper;
import com.blackcat.recode.entity.Branch;
import com.blackcat.recode.entity.DeployRecode;
import com.blackcat.recode.dao.DeployRecodeMapper;
import com.blackcat.recode.entity.Project;
import com.blackcat.recode.entity.UpdateRecode;
import com.blackcat.recode.service.DeployRecodeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blackcat.recode.utils.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * <p> 客户部署记录表 服务实现类
 * @author blackcat 2024-09-13
 */
@Service
public class DeployRecodeServiceImpl extends ServiceImpl<DeployRecodeMapper, DeployRecode> implements DeployRecodeService {

    @Autowired
    private DeployRecodeMapper deployRecodeMapper;
    @Autowired
    private UpdateRecodeMapper updateRecodeMapper;


    @Override
    public AjaxResult getOne(String id) {
        DeployRecode deployRecode = deployRecodeMapper.selectById(id);
        UpdateRecode updateRecode = updateRecodeMapper.selectById(deployRecode.getUpdateId());
        deployRecode.setProjectId(updateRecode.getProjectId());
        deployRecode.setServiceId(updateRecode.getServiceId());
        deployRecode.setBranchId(updateRecode.getBranchId());
        return AjaxResult.success(deployRecode);
    }

    @Override
    public AjaxResult getList(Integer pageNow, Integer pageSize, String customerId, String projectId, String serviceId) {
        QueryWrapper<DeployRecode> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(customerId)) {
            queryWrapper.like("customer_id", customerId);
        }
        if (StringUtils.isNotBlank(projectId)) {
            queryWrapper.eq("project_id", projectId);
        }
        if (StringUtils.isNotBlank(serviceId)) {
            queryWrapper.eq("service_id", serviceId);
        }
        queryWrapper.orderByDesc("create_time");
        Page<DeployRecode> page = new Page<>(pageNow, pageSize);
        Page<DeployRecode> result = deployRecodeMapper.selectPage(page, queryWrapper);
        if (CollectionUtils.isNotEmpty(result.getRecords())) {
            result.getRecords().forEach(i -> {
                UpdateRecode updateRecode = updateRecodeMapper.selectById(i.getUpdateId());
                if (updateRecode != null) {
                    i.setProjectId(updateRecode.getProjectId());
                    i.setServiceId(updateRecode.getServiceId());
                    i.setBranchId(updateRecode.getBranchId());
                }
            });
        }
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult edit(DeployRecode deployRecode) {
        if (deployRecode.getId() == null) {
            deployRecodeMapper.insert(deployRecode);
        }else {
            deployRecodeMapper.updateById(deployRecode);
        }
        return AjaxResult.success();
    }
}
